package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsCollect;
import com.qf.service.AdminCollectService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RequestMapping("/admin/collect")
@CrossOrigin
@RestController
public class AdminCollectController {

    @Autowired
    private AdminCollectService adminCollectService;

    /**
     * 查询会员收藏列表
     * @param page
     * @param limit
     * @param userId
     * @param valueId
     * @param sort
     * @param order
     * @return
     *
     * {
     * 	"errno": 0,
     * 	"data": {
     * 		"total": 44,
     * 		"items": [
     * 			{
     * 				"id": 81,
     * 				"userId": 194,
     * 				"valueId": 23854,
     * 				"type": 0,
     * 				"addTime": "2019-12-20 11:12:13",
     * 				"updateTime": "2019-12-20 11:12:13",
     * 				"deleted": false
     * 			},
     * 			{
     * 				"id": 80,
     * 				"userId": 188,
     * 				"valueId": 23855,
     * 				"type": 0,
     * 				"addTime": "2019-12-19 14:28:23",
     * 				"updateTime": "2019-12-19 14:28:23",
     * 				"deleted": false
     * 			}
     * 		]
     * 	},
     * 	"errmsg": "成功"
     * }
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:collect:list")
    @RequiresPermissionsDesc(menu = "会员收藏",button = "会员收藏列表")
    public Object collectList(@RequestParam(defaultValue = "1") Integer page,
                              @RequestParam(defaultValue = "20") Integer limit,
                              Integer userId,Integer valueId,
                              String sort,String order){

        IPage<DtsCollect> iPage = adminCollectService.selectCollectList(page,limit,userId,valueId,sort,order);

        long total = iPage.getTotal();
        List<DtsCollect> records = iPage.getRecords();

        DtsCollect[] items = new DtsCollect[records.size()];

        for (int i = 0; i < records.size(); i++) {
            items[i] = records.get(i);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("items", items);
        data.put("total", total);

        return ResponseUtil.ok(data);
    }
}
